/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "S_PhysicsBullet.h"

#include <ppl.h>
#include <list>

#include "物体/S_物体.h"


static std::vector<btVector3> g车辆检测射线(10);




S_PhysicsBullet::S_PhysicsBullet() {
	
	vec3 d = { 0.0f,0.0f,-20.0f };
	for (uint8 i = 0; i < 8; ++i) {
		//C_Mat44 mat = C_Mat44::kIdentity;
		C_Mat44 mat = RotationMatrix(M_角度转弧度(45.0 * i), {0.0f,1.0f,0.0});

		vec3 r = mat * d;
		g车辆检测射线[i] = {r.x, r.y, r.z};
	}

	C_Mat44 mat = RotationMatrix(M_角度转弧度(20.0), { 0.0f,1.0f,0.0 });
	vec3 r = mat * d;
	g车辆检测射线[8] = { r.x, r.y, r.z };
	mat = RotationMatrix(M_角度转弧度(-20.0), { 0.0f,1.0f,0.0 });
	r = mat * d;
	g车辆检测射线[9] = { r.x, r.y, r.z };
}

S_PhysicsBullet::~S_PhysicsBullet() {
	
}

void S_PhysicsBullet::f_Update() {
	uint16 num	= m_Core.m_物理域空间.size();
	auto* world	= m_Core.m_物理域空间.data();
	//return;

	if (mf_车辆操作更新) {

#ifdef _DEBUG
		for (auto& 车 : m_车) {
			mf_车辆操作更新(车);
		}
#else
		Concurrency::parallel_for_each(m_车.begin(), m_车.end(), [&](S_Bullet车* 车) {
			mf_车辆操作更新(车);
		});
#endif // DEBUG

		
	}
	
	


	for (uint32 i = 0; i < num; ++i) {
		auto& 物理世界 = world[i];
		物理世界->stepSimulation(m_步进时间, 3);

		if (物理世界->getConstraintSolver()->getSolverType() == BT_MLCP_SOLVER) {
			btMLCPSolver* sol = (btMLCPSolver*)物理世界->getConstraintSolver();
			int numFallbacks = sol->getNumFallbacks();
			if (numFallbacks) {
				static int totalFailures = 0;
				totalFailures += numFallbacks;
				//printf("MLCP solver failed %d times, falling back to btSequentialImpulseSolver (SI)\n", totalFailures);
			}
			sol->setNumFallbacks(0);
		}
		
		
		uint32 物理体数量 = 物理世界->getNumCollisionObjects();


		for (uint32 phyID = 0; phyID < 物理体数量; ++phyID) {
			btCollisionObject* 物体 = 物理世界->getCollisionObjectArray()[phyID];

			if (物体->getActivationState() != DISABLE_DEACTIVATION || 物体->isActive()) {
				btTransform 变换 = 物体->getWorldTransform();
				btVector3 位置 = 变换.getOrigin();
				btQuaternion 旋转 = 变换.getRotation();

				S_物体* 绑定物体 = (S_物体*)物体->getUserPointer();
				if (绑定物体) {
					//绑定物体->f_set位置(位置.x(), 位置.y(), 位置.z());
					//绑定物体->f_set旋转(f_graph_四元数转欧拉角(vec4{旋转.x(), 旋转.y(), 旋转.z(), 旋转.w()}));
					S_Tranform t;
					f_TranformInit(t);
					t.location = { 位置.x(), 位置.y(), 位置.z() };
					t.rotation = f_graph_四元数转欧拉角(vec4{ 旋转.x(), 旋转.y(), 旋转.z(), 旋转.w() });
					auto mat = f_mat44_构建XYZ变换矩阵(t);

					f_ob_set矩阵(绑定物体, mat, 绑定物体->m_实例ID);
				}
			}
		}

	}
}

void S_PhysicsBullet::f_应用物理变换() {
	uint16 num = m_Core.m_物理域空间.size();
	auto* world = m_Core.m_物理域空间.data();

	//return;

	//print positions of all objects
	for (uint32 i = 0; i < num; ++i) {
		auto& 空间 = world[i];

		//Concurrency::parallel_invoke(
			//[&] {
				uint32 objNum = 空间->getNumCollisionObjects();

				for (uint32 j = 0; j < objNum; ++j) {
					btCollisionObject* obj = 空间->getCollisionObjectArray()[j];
					btRigidBody* body = btRigidBody::upcast(obj);
					btTransform trans;

					if (body && body->getMotionState()) {
						body->getMotionState()->getWorldTransform(trans);
					}
					else {
						trans = obj->getWorldTransform();
					}
					//printf("world pos object %d = %f,%f,%f\n", j, float(trans.getOrigin().getX()), float(trans.getOrigin().getY()), float(trans.getOrigin().getZ()));
					//trans.getOpenGLMatrix();

					S_物体* user = (S_物体*)body->getUserPointer();
					if (user) {
						Mat44f mat;
						trans.getOpenGLMatrix((float32*)&mat);
						user->f_set矩阵(&mat);
					}

				}
			//},




			//[&] {
				for (auto& 车 : m_车) {
					//Concurrency::parallel_for_each(m_车.begin(), m_车.end(), [&](S_Bullet车* 车) {
					for (uint32 w = 0; w < 4; ++w) {
						车->m_vehicle->updateWheelTransform(w, true);

						Mat44f mat;
						车->m_vehicle->getWheelTransformWS(w).getOpenGLMatrix((float32*)&mat);
						//btTransform trans = 车->m_vehicle->getWheelInfo(w).m_worldTransform;
						//trans.getOpenGLMatrix((float32*)&mat);
						((S_物体*)车->m_车轮对象[w])->f_set矩阵(&mat);
					}
					//});


					auto pos = 车->m_vehicle->getChassisWorldTransform().getOrigin();

//#ifdef _DEBUG
					for (uint8 r = 0; r < 10; ++r) {
//#else
//					Concurrency::parallel_for<uint16>(0, 8, [&](uint16 r) {
//#endif // DEBUG
						btVector3 to = pos + g车辆检测射线[r];
						//from + to;
						btCollisionWorld::ClosestRayResultCallback closestResults(pos, to);
						closestResults.m_flags |= btTriangleRaycastCallback::kF_FilterBackfaces;

						空间->rayTest(pos, to, closestResults);
						if (closestResults.hasHit()) {
							btVector3 co = pos.lerp(to, closestResults.m_closestHitFraction);
							车->m_射线碰撞点[r] = vec3{ float32(co.x()), float32(co.y()), float32(co.z()) };
						}
						else {
							车->m_射线碰撞点[r] = vec3{ float32(to.x()), float32(to.y()), float32(to.z()) };
						}
//#ifdef _DEBUG
					}
//#else
//					});
//#endif // DEBUG
					
				}
			//},



				
			/************************************************************************************
			*                         全局碰撞回调
			*
			*************************************************************************************/
			//[&] {
				if (m_全局碰撞回调) {
					空间->performDiscreteCollisionDetection();

					//std::list<btCollisionObject*> m_collisionObjects;
					int numManifolds = 空间->getDispatcher()->getNumManifolds();

					for (int i = 0; i < numManifolds; i++)
					{
						btPersistentManifold* contactManifold = 空间->getDispatcher()->getManifoldByIndexInternal(i);
						btCollisionObject* obA = (btCollisionObject*)(contactManifold->getBody0());
						btCollisionObject* obB = (btCollisionObject*)(contactManifold->getBody1());

						S_碰撞信息 info = {0};

						info.物体A = obA->getUserPointer();
						info.物体B = obB->getUserPointer();

						int numContacts = contactManifold->getNumContacts();
						for (int j = 0; j < numContacts; j++) {
							btManifoldPoint& pt = contactManifold->getContactPoint(j);
							if (pt.getDistance() <= 0.f) {
								btVector3 posA = pt.getPositionWorldOnA();
								btVector3 posB = pt.getPositionWorldOnB();

								info.坐标A = { posA.getX(), posA.getY(), posA.getZ() };
								info.坐标B = { posB.getX(), posB.getY(), posB.getZ() };

								info.冲量 = pt.getAppliedImpulse();
								info.周期 = pt.getLifeTime();

								btRigidBody* a = static_cast<btRigidBody*>(obA);
								btRigidBody* b = static_cast<btRigidBody*>(obB);

								if (a) {
									info.速度A = a->getLinearVelocity().length();
								}
								
								if (b) {
									info.速度B = b->getLinearVelocity().length();
								}
								

								//a->getLinearFactor();
								if (m_全局碰撞回调(info)) {
									return;
								}
								//m_collisionObjects.push_back(obA);
								//m_collisionObjects.push_back(obB);
								//printf("%d A -> {%f, %f, %f}\n", i, posA.getX(), posA.getY(), posA.getZ()); // 碰撞点
								//printf("%d B -> {%f, %f, %f}\n", i, posB.getX(), posB.getY(), posB.getZ());
							}
						}
					}


				}
				
			//}
		//);


	}
	
	
}

void S_PhysicsBullet::f_取更新物体(std::vector<Mat44f>& mat, std::vector<void*>& userData) {
	uint16 num = m_Core.m_物理域空间.size();
	auto* world = m_Core.m_物理域空间.data();


	uint32 begin = 0;
	//print positions of all objects
	for (uint32 i = 0; i < num; ++i) {
		uint32 objNum = world[i]->getNumCollisionObjects();


		userData.resize(mat.size() + objNum);
		mat.resize(mat.size() + objNum);

		auto* p_Objs = userData.data();
		auto* p_Mat = mat.data();

		

		for (uint32 j = 0; j < objNum; ++j) {
			btCollisionObject* obj = world[i]->getCollisionObjectArray()[j];
			btRigidBody* body = btRigidBody::upcast(obj);
			btTransform trans;

			if (body && body->getMotionState()) {
				body->getMotionState()->getWorldTransform(trans);
			}
			else {
				trans = obj->getWorldTransform();
			}

			//printf("world pos object %d = %f,%f,%f\n", j, float(trans.getOrigin().getX()), float(trans.getOrigin().getY()), float(trans.getOrigin().getZ()));
			trans.getOpenGLMatrix((float32*)&p_Mat[begin+j]);
			p_Objs[begin + j] = body->getUserPointer();
			
		}

		begin = mat.size();
	}

}

void S_PhysicsBullet::f_取碰撞物体(std::vector<S_碰撞信息>& objs, uint32 scenekey) {
	auto scene = m_Core.m_物理域空间[scenekey];
	scene->performDiscreteCollisionDetection();

	std::list<btCollisionObject*> m_collisionObjects;
	int numManifolds = scene->getDispatcher()->getNumManifolds();

	for (int i = 0; i < numManifolds; i++) {
		btPersistentManifold* contactManifold = scene->getDispatcher()->getManifoldByIndexInternal(i);
		btCollisionObject* obA = (btCollisionObject*)(contactManifold->getBody0());
		btCollisionObject* obB = (btCollisionObject*)(contactManifold->getBody1());

		int numContacts = contactManifold->getNumContacts();
		for (int32 j = 0; j < numContacts; j++) {
			btManifoldPoint& pt = contactManifold->getContactPoint(j);
			if (pt.getDistance() <= 0.f) {
				m_collisionObjects.push_back(obA);
				m_collisionObjects.push_back(obB);
				btVector3 posA = pt.getPositionWorldOnA();
				btVector3 posB = pt.getPositionWorldOnB();
				//printf("%d A -> {%f, %f, %f}\n", i, posA.getX(), posA.getY(), posA.getZ()); // 碰撞点
				//printf("%d B -> {%f, %f, %f}\n", i, posB.getX(), posB.getY(), posB.getZ());
				
				pt.getLifeTime();

				S_碰撞信息 info;
				info.距离 = pt.getDistance();
				info.坐标A = { posA.x(), posA.y(), posA.z() };
				info.坐标B = { posB.x(), posB.y(), posB.z() };
				info.冲量 = pt.getAppliedImpulse();
				info.周期 = pt.getLifeTime();

				info.物体A = const_cast<void*>((void*)obA->getUserPointer());
				info.物体B = const_cast<void*>((void*)obB->getUserPointer());

				objs.push_back(info);
			}
		}
	}

	//btPoint2PointConstraint()
}


void S_PhysicsBullet::f_取更新物体(std::vector<std::vector<Mat44f>>& mat, std::vector<std::vector<void*>>& userData) {
	

}

void S_PhysicsBullet::f_添加物理体(S_物理体* body, uint32 scenekey) {
	switch(body->m_物理体类型) {
	case E_物理体类型::E_物理体_刚体:
		m_Core.m_物理域空间[scenekey]->addRigidBody(dynamic_cast<S_Bullet刚体*>(body)->m_bt刚体);
		break;
	case E_物理体类型::E_物理体_柔体:
		break;
	case E_物理体类型::E_物理体_布料:
		break;
	case E_物理体类型::E_物理体_静态:
		m_Core.m_物理域空间[scenekey]->addRigidBody(dynamic_cast<S_Bullet静态碰撞*>(body)->m_Body);
		break;
	case E_物理体类型::E_物理体_控制:
		m_Core.m_物理域空间[scenekey]->addRigidBody(dynamic_cast<S_Bullet控制*>(body)->m_Body);
		break;

	case E_物理体类型::E_物理体_车辆:
		{
			auto* car = dynamic_cast<S_Bullet车*>(body);
			if (car) {
				m_车.push_back(car);
				m_Core.m_物理域空间[scenekey]->addRigidBody(car->m_Body);
				m_Core.m_物理域空间[scenekey]->addVehicle(car->m_vehicle);
			}
		}
		break;
	}
	
}

void S_PhysicsBullet::f_移除物理体(S_物理体* body, uint32 scenekey) {
	switch (body->m_物理体类型) {
	case E_物理体类型::E_物理体_刚体:
		m_Core.m_物理域空间[scenekey]->removeRigidBody(dynamic_cast<S_Bullet刚体*>(body)->m_bt刚体);
		break;
	case E_物理体类型::E_物理体_静态:
		m_Core.m_物理域空间[scenekey]->removeRigidBody(dynamic_cast<S_Bullet静态碰撞*>(body)->m_Body);
		break;
	}
}

void S_PhysicsBullet::f_添加物理体碰撞回调(S_物理体* body, uint32 scenekey) {
	//m_Core.m_物理域空间[scenekey]->contactTest((btRigidBody*)body->f_getBody(), );


	

}

void S_PhysicsBullet::f_重置变换() {
	uint16 num = m_Core.m_物理域空间.size();
	auto* world = m_Core.m_物理域空间.data();

	for (uint32 i = 0; i < num; ++i) {
		auto& 物理世界 = world[i];
		uint32 物理体数量 = 物理世界->getNumCollisionObjects();


		for (uint32 phyID = 0; phyID < 物理体数量; ++phyID) {
			btCollisionObject* 物体 = 物理世界->getCollisionObjectArray()[phyID];

			if (物体->getActivationState() != DISABLE_DEACTIVATION || 物体->isActive()) {
				
				S_物体* 绑定物体 = (S_物体*)物体->getUserPointer();
				if (绑定物体) {
					
					
					if (物体->getInternalType() == btCollisionObject::CO_RIGID_BODY) {
						btTransform 变换;

						vec4 q = f_graph_旋转角转四元数(绑定物体->m_变换.rotation);
						变换.setOrigin({ 绑定物体->m_变换.location.x, 绑定物体->m_变换.location.y, 绑定物体->m_变换.location.z });
						变换.setRotation({ q.x, q.y, q.z, q.w });
						物体->setWorldTransform(变换);
						物体->activate();


						//switch (物体->m_Type) {
						//	case E_物理体类型::E_物理体_刚体:
						//		break;
						//	case E_物理体类型::E_物理体_柔体:
						//		break;
						//	case E_物理体类型::E_物理体_布料:
						//		break;
						//	case E_物理体类型::E_物理体_静态:
						//		break;
						//	case E_物理体类型::E_物理体_控制:
						//		break;
						//
						//	case E_物理体类型::E_物理体_车辆: {
						//	}
						//	break;
						//}

						btRigidBody* 刚体 = (btRigidBody*)(物体);
						刚体->setLinearVelocity(btVector3(0, 0, 0));
						刚体->setAngularVelocity(btVector3(0, 0, 0));
					}
					

					auto mat = f_mat44_构建XYZ变换矩阵(绑定物体->m_变换);
					f_ob_set矩阵(绑定物体, mat, 绑定物体->m_实例ID);
				}
			}
		}
	}
}





// 碰撞检测回调
struct MyColCallBack : btCollisionWorld::ContactResultCallback
{
public:
	btScalar addSingleResult(
		btManifoldPoint& cp,
		const btCollisionObjectWrapper* colObj0Wrap,
		int partId0,
		int index0,
		const btCollisionObjectWrapper* colObj1Wrap,
		int partId1,
		int index1)
	{
		btVector3 posA = cp.getPositionWorldOnA();
		btVector3 posB = cp.getPositionWorldOnB();
		//printf("col pos for A {%f, %f, %f}\n", posA.getX(), posA.getY(), posA.getZ());
		//printf("col pos for B {%f, %f, %f}\n", posB.getX(), posB.getY(), posB.getZ());

		return btScalar(0.f);
	};
};






/*int wheelIndex = 2;
			车->m_vehicle->applyEngineForce(0.02, wheelIndex);
			车->m_vehicle->setBrake(0.0, wheelIndex);
			wheelIndex = 3;
			车->m_vehicle->applyEngineForce(0.04, wheelIndex);
			车->m_vehicle->setBrake(0.0, wheelIndex);

			wheelIndex = 0;
			车->m_vehicle->setSteeringValue(0.3, wheelIndex);
			车->m_vehicle->applyEngineForce(0.0, wheelIndex);
			wheelIndex = 1;
			车->m_vehicle->setSteeringValue(0.3, wheelIndex);
			车->m_vehicle->applyEngineForce(0.0, wheelIndex);*/

static void on_销毁物理体(S_Object* obj) {
	S_物理体* 物理体 = (S_物理体*)obj;

	
	switch (物理体->m_几何体->m_Type) {
		case E_物理几何体类型::E_碰撞几何_方体: {
			auto s = ((btBoxShape*)(物理体->m_Data));
			if (s) {
			//	delete s;
			}
			break;
		}
		case E_物理几何体类型::E_碰撞几何_网格: {
			auto s = ((btTriangleMesh*)(物理体->m_Data));
			if (s) {
				delete s;
			}
			break;
		}
	}
}

#include <btBulletDynamicsCommon.h>
void MyCollisionCallback(btCollisionObject* colObj0, btCollisionObject* colObj1, const btCollisionShape* shape) {
	// 在这里处理碰撞，例如获取刚体信息、碰撞形状等
	std::cout<< colObj0 << "\n";
}

void f_bullet_构建物理体(S_PhysicsBullet* 解算器, S_PhyBodyArray* body) {
	auto& 物理空间 = 解算器->m_Core.m_物理域空间.front();

	for (uint32 i = 0; i < body->count; ++i) {
		S_物理体* 物理体 = body->ptr_userData[i];

		
		btCollisionShape* 碰撞形状 = nullptr;
		btVector3 惯性(0, 0, 0);


		S_物体* 绑定物体 = nullptr;
		S_物理几何体& 几何体 = *body->ptr_userData[i]->m_几何体;
		S_Tranform& 变换 = ((S_物体*)几何体.m_绑定内置物体)->m_变换;

		switch (几何体.m_Type) {
			case E_物理几何体类型::E_碰撞几何_方体: {
				S_物理几何_方体* box = (S_物理几何_方体*)body->ptr_userData[i]->m_几何体->m_Data;

				if (box->m_Data) {
					//delete box->m_Data;
				}
				btBoxShape* s = new btBoxShape(btVector3(box->大小.x, box->大小.y, box->大小.z));
				box->m_Data = s;

				碰撞形状 = (btCollisionShape*)box->m_Data;
				碰撞形状->calculateLocalInertia(物理体->m_几何体->g_材质.m_重量, 惯性);

				绑定物体 = (S_物体*)body->ptr_userData[i]->m_几何体->m_绑定内置物体;
				break;
			}
			case E_物理几何体类型::E_碰撞几何_平面: {
				S_物理几何_平面* plane = (S_物理几何_平面*)body->ptr_userData[i]->m_几何体->m_Data;

				if (plane->m_Data) {
					//delete plane->m_Data;
				}
				btStaticPlaneShape* s = new btStaticPlaneShape(btVector3(plane->方向.x, plane->方向.y, plane->方向.z), 1);
				plane->m_Data = s;

				
				碰撞形状 = (btCollisionShape*)plane->m_Data;
				//碰撞形状->calculateLocalInertia(物理体->m_几何体->m_材质.m_重量, 惯性);
				//绑定物体 = (S_物体*)body->ptr_userData[i]->m_几何体->m_绑定物体;
				绑定物体 = (S_物体*)几何体.m_绑定内置物体;
				break;
			}

			case E_物理几何体类型::E_碰撞几何_网格: {
				S_物理几何_网格* me = (S_物理几何_网格*)几何体.m_Data;

				
				std::vector<btVector3> vertices(me->顶点数量);
				for (uint32 i = 0; i < me->顶点数量; ++i) {
					vertices[i].setValue(me->顶点[i].vert.x, me->顶点[i].vert.y, me->顶点[i].vert.z);
				}
				std::vector<int32> indexs(me->索引数量);
				for (uint32 i = 0; i < me->顶点数量; ++i) {
					indexs[i] = me->索引[i];
				}
				
				int32 indexNum = me->索引数量 / 3;
				int32 vertNum = me->顶点数量;

				btTriangleIndexVertexArray* indexVertexArrays = new btTriangleIndexVertexArray(
					indexNum,
					indexs.data(),
					3 * sizeof(int32),
					me->顶点数量,
					(btScalar*)&vertices[0].x(),
					3 * sizeof(btScalar));

				//btBvhTriangleMeshShape* s = new btBvhTriangleMeshShape(mesh, true);
				btBvhTriangleMeshShape* s = new btBvhTriangleMeshShape(indexVertexArrays, true, true);
				s->buildOptimizedBvh();
				me->m_Data = s;

				//s->calculateLocalInertia(几何体.m_材质.m_重量, 惯性);
				碰撞形状 = s;
				break;
			}
		}


		btTransform 初始变换;
		初始变换.setIdentity();
		初始变换.setOrigin(btVector3(变换.location.x, 变换.location.y, 变换.location.z));

		btQuaternion 初始旋转;
		初始旋转.setEulerZYX(变换.rotation.x, 变换.rotation.y, 变换.rotation.z);
		初始变换.setRotation(初始旋转);
		

		btDefaultMotionState* 初始运动状态 = new btDefaultMotionState(初始变换);
		

		float32 重量 = 物理体->m_几何体->g_材质.m_重量;
		btRigidBody* rigidBody = nullptr;

		//if (!物理体->m_Data) {
		switch (物理体->m_物理体类型) {
			case E_物理体类型::E_物理体_静态: 
				重量 = 0;
				//btRigidBody::btRigidBodyConstructionInfo 刚体构建信息(重量, 初始运动状态, 碰撞形状, 惯性);
				//break;

			case E_物理体类型::E_物理体_刚体: {
				//btDefaultMotionState 初始运动状态(初始变换);
				btRigidBody::btRigidBodyConstructionInfo 刚体构建信息(重量, 初始运动状态, 碰撞形状, 惯性);
				// 创建刚体
				rigidBody = new btRigidBody(刚体构建信息);

				break;
			}

			default:
				break;
		}

		if (rigidBody) {
			if (重量 <= 0) {
				rigidBody->setCollisionFlags(rigidBody->getCollisionFlags() | btCollisionObject::CF_STATIC_OBJECT);
			}
			

			//btCollisionCallbackData* callbackData = new CollisionCallbackData;
			//callbackData->callback = MyCollisionCallback;
			

			if (!物理体->m_绑定物体) {
				rigidBody->setUserPointer(绑定物体);
			}
			else {
				rigidBody->setUserPointer(物理体->m_绑定物体);
			}
			物理体->m_Data = rigidBody;
			物理空间->addRigidBody(rigidBody);
		}
			
		物理体->mf_销毁 = on_销毁物理体;
		//}
	}
}

void f_bullet_重置引擎(S_PhysicsBullet* 解算器) {
	auto& 物理空间 = 解算器->m_Core.m_物理域空间.front();

	uint32 场景物体数量 = 物理空间->getNumCollisionObjects();
	auto& objectArray = 物理空间->getCollisionObjectArray();

	for (int32 i = 0; i < 场景物体数量; ++i) {
		btCollisionObject* pObject = objectArray[0];
		物理空间->removeCollisionObject(pObject);

		//on_销毁物理体(pObject);
		delete pObject;
	}

	for (int32 i = 0; i < 场景物体数量; ++i) {
		
	}
}





